package com.qlu.casualtalk.controller;

import com.qlu.casualtalk.constant.JwtConstant;
import com.qlu.casualtalk.constant.MessageConstant;
import com.qlu.casualtalk.constant.RegexConstant;
import com.qlu.casualtalk.domain.dto.UserLoginDTO;
import com.qlu.casualtalk.domain.dto.UserRegistrationDTO;
import com.qlu.casualtalk.domain.po.User;
import com.qlu.casualtalk.domain.vo.UserBasicInfoVO;
import com.qlu.casualtalk.enums.Status;
import com.qlu.casualtalk.server.UserService;
import com.qlu.casualtalk.util.Md5Util;
import com.qlu.casualtalk.util.Result;
import com.qlu.casualtalk.util.ThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
@RequiredArgsConstructor
public class UserController {


    private final UserService userService;

    /**
     * 用户注册
     * @param userRegistrationDTO
     * @return Result
     */
    @PostMapping("/register")
    public Result register(@RequestBody @Validated UserRegistrationDTO userRegistrationDTO){

        //检查账号是否已存在

        User userByAccount=userService.findUserByAccount(userRegistrationDTO.getAccount());
        if (userByAccount!=null){
            return Result.error(MessageConstant.ACCOUNT_ALREADY_EXISTS);
        }



        //检查手机号是否已注册
        if(userRegistrationDTO.getPhone()!=null&&!userRegistrationDTO.getPhone().equals("")){
            User userByPhone=userService.findUserByPhone(userRegistrationDTO.getPhone());
            if (userByPhone!=null){
                return Result.error(MessageConstant.PHONE_ALREADY_REGISTERED);
            }
        }

        //检查邮箱是否已注册
        if(userRegistrationDTO.getEmail()!=null&&!userRegistrationDTO.getEmail().equals("")){
            User userByEmail=userService.findUserByEmail(userRegistrationDTO.getEmail());
            if (userByEmail!=null){
                return Result.error(MessageConstant.EMAIL_ALREADY_REGISTERED);
            }
        }


        //注册用户
        userService.register(userRegistrationDTO);
        return Result.success();

    }

    /**
     * 用户登录
     * 前端传入的账号可以是账号、邮箱或手机号
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody @Validated UserLoginDTO userLoginDTO){
        User user=null;

        // 先判断账号是否符合格式
        if(userLoginDTO.getAccountOrEmailOrPhone().matches(RegexConstant.ACCOUNT_REGEX)){
            user=userService.findUserByAccount(userLoginDTO.getAccountOrEmailOrPhone());
        }else if(userLoginDTO.getAccountOrEmailOrPhone().matches(RegexConstant.EMAIL_REGEX)){
            user=userService.findUserByEmail(userLoginDTO.getAccountOrEmailOrPhone());
        }else if(userLoginDTO.getAccountOrEmailOrPhone().matches(RegexConstant.PHONE_REGEX)){
            user=userService.findUserByPhone(userLoginDTO.getAccountOrEmailOrPhone());
        }
        else{
            // 账号格式不正确
            return Result.error(MessageConstant.ACCOUNT_OR_PHONE_OR_EMAIL_FORMAT_ERROR);
        }

        // 判断用户是否存在
        if(user==null){
            return Result.error(MessageConstant.USER_NOT_FOUND);
        }
        else {
            // 判断用户是否被锁定
            if(user.getStatus()== Status.suspended){
                return Result.error(MessageConstant.USER_LOCKED);
            }
            // 判断密码是否正确
            else if(!Md5Util.checkPassword(userLoginDTO.getPassword(),user.getPassword())){
                return Result.error(MessageConstant.PASSWORD_ERROR);
            }
        }
        //返回token
        return Result.success(userService.login(user));
    }

    @PostMapping("/logout")
    public Result logout(){
        ThreadLocalUtil.remove();
        return Result.success();
    }

    /**
     * 获取当前登录用户id
     * @return
     */
    @GetMapping("/localUserInfo")
    public Result localUserInfo(){
        Map<String,Object> o = ThreadLocalUtil.get();
        return Result.success(o.get(JwtConstant.userIdInThreadLocal));
    }

    /**
     * 获取用户基本信息
     * @return
     */
    @GetMapping("/basicUserInfo")
    public Result basicUserInfo(){
        UserBasicInfoVO userBasicInfoVO=userService.getUserBasicInfo();

        return Result.success(userBasicInfoVO);
    }

    /**
     * 修改用户信息
     * @param item 修改项
     * @param value 修改值
     * @return
     */
    @PutMapping("/updateUserInfo")
    public Result updateUserInfo(String item,String value){
        userService.updateUserInfo(item,value);
        return Result.success();
    }

}
